var express = require('express');
var router = express.Router();
var { conn, isError } = require("../utils");
var { ObjectID } = require("mongodb");
var { waterfall } = require("async");

router.post('/register', (req, res) => {
  let body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    waterfall([
      (cb) => {
        ky_user_info.findOne({ utel: body.utel }, {}, (err, result) => {
          isError(err, res, db);
          if (result) {
            cb(null, true);
          } else {
            cb(null, false);
          }
        });
      },
      (arg, cb) => {
        if (arg) {
          cb(null, {
            msg: "手机号码已被注册",
            code: 0
          })
        } else {
          ky_user_info.insert(body, (err, result) => {
            isError(err, res, db);
            if (result) {
              cb(null, {
                msg: "注册成功",
                code: 1
              })
            } else {
              cb(null, {
                msg: "注册失败",
                code: 0
              })
            }
          });
        }
      }
    ], (err, result) => {
      isError(err, res, db);
      res.json(result);
      db.close();
    })
  })
});

router.post("/login", (req, res) => {
  var body = req.body;
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.findOne(body, {}, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "登录成功",
          code: 1,
          uid: result._id
        });
      } else {
        res.json({
          msg: "用户名或密码错误",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/user", (req, res) => {
  var body = req.body;
  body._id = ObjectID(body._id);
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.findOne(body, {}, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          result,
          code: 1
        });
      } else {
        res.json({
          msg: "用户不存在",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/theme", (req, res) => {
  var body = req.body;
  body._id = ObjectID(body._id);
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.updateOne({ _id: body._id, state: 1 }, { $addToSet: { uatt_theme: body.uatt_theme } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "关注成功",
          code: 1
        });
      } else {
        res.json({
          msg: "关注失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/remove-theme", (req, res) => {
  var body = req.body;
  body._id = ObjectID(body._id);
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.updateOne({ _id: body._id, state: 1 }, { $pull: { uatt_theme: body.uatt_theme } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "取消关注成功",
          code: 1
        });
      } else {
        res.json({
          msg: "取消关注失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/pgc", (req, res) => {
  var body = req.body;
  body._id = ObjectID(body._id);
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.updateOne({ _id: body._id, state: 1 }, { $addToSet: { uatt_pgc: body.uatt_pgc } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "关注成功",
          code: 1
        });
      } else {
        res.json({
          msg: "关注失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/remove-pgc", (req, res) => {
  var body = req.body;
  body._id = ObjectID(body._id);
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.updateOne({ _id: body._id, state: 1 }, { $pull: { uatt_pgc: body.uatt_pgc } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "取消关注成功",
          code: 1
        });
      } else {
        res.json({
          msg: "取消关注失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/like", (req, res) => {
  var body = req.body;
  body._id = ObjectID(body._id);
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.updateOne({ _id: body._id, state: 1 }, { $addToSet: { ulike: body.ulike } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "喜欢成功",
          code: 1
        });
      } else {
        res.json({
          msg: "喜欢失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

router.post("/remove-like", (req, res) => {
  var body = req.body;
  body._id = ObjectID(body._id);
  conn((err, db) => {
    isError(err, res, db);
    var ky_user_info = db.collection("ky_user_info");
    ky_user_info.updateOne({ _id: body._id, state: 1 }, { $pull: { ulike: body.ulike } }, (err, result) => {
      isError(err, res, db);
      if (result) {
        res.json({
          msg: "取消喜欢成功",
          code: 1
        });
      } else {
        res.json({
          msg: "取消喜欢失败",
          code: 0
        });
      }
      db.close();
    });
  })
})

module.exports = router;
